package com.cheng.imagerelated.preview.adpter;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.cheng.imagerelated.R;

import java.util.ArrayList;
import java.util.List;

public class PreviewImageAdapter extends RecyclerView.Adapter<PreviewImageAdapter.ImageHolder> {

    private Context mContext;
    private List<String> mImgList;


    public interface OnItemClcikLitener {
        void OnItemClcik(PreviewImageAdapter previewImageAdapter, View iteView, int position);
    }

    public OnItemClcikLitener onItemClcikLitener;

    public void setOnItemClcikLitener(OnItemClcikLitener onItemClcikLitener) {
        this.onItemClcikLitener = onItemClcikLitener;
    }

    public PreviewImageAdapter(Context mContext, List<String> mImgList) {
        this.mContext = mContext;
        this.mImgList = mImgList;
    }

    public List<String> getData() {
        return mImgList;
    }

    @Override
    public int getItemCount() {
        return mImgList.size();
    }

    @NonNull
    @Override
    public ImageHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        final ImageHolder imageHolder = new ImageHolder(LayoutInflater.from(mContext).inflate(R.layout.preview_item, parent, false));
        imageHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClcikLitener != null) {
                    onItemClcikLitener.OnItemClcik(PreviewImageAdapter.this, imageHolder.itemView, imageHolder.getLayoutPosition());
                }
            }
        });
        return imageHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ImageHolder holder, int position) {
        String path = mImgList.get(position);
        Glide.with(mContext).setDefaultRequestOptions(new RequestOptions()
                .dontTransform()
                .placeholder(R.drawable.ic_image)
                .error(R.drawable.ic_img_load_fail)
                .override(800, 1200))
                .load(path)
                .into(holder.imageView);
        boolean b = position > mLastPosition;
        addInAnimation(holder.itemView, b);
        mLastPosition = position;
    }

    private int mLastPosition = -1;

    /**
     * 将动画对象加入集合中  根据左右滑动加入不同
     */
    private void addInAnimation(View view, boolean left) {
        List<Animator> list = new ArrayList<>();
//        if (left) {
//            list.add(ObjectAnimator.ofFloat(view, "translationX", -view.getMeasuredWidth() * 2, 0));
//        } else {
//            list.add(ObjectAnimator.ofFloat(view, "translationX", view.getMeasuredWidth() * 2, 0));
//        }
//        list.add(ObjectAnimator.ofFloat(view, "translationY", view.getMeasuredHeight() / 2, 0));
        list.add(ObjectAnimator.ofFloat(view, "alpha", 0, 1));
//        list.add(ObjectAnimator.ofFloat(view, "scaleX", 0.25f, 1));
//        list.add(ObjectAnimator.ofFloat(view, "scaleY", 0.25f, 1));
        startAnimation(list);
    }

    /**
     * 开启动画
     */
    private void startAnimation(List<Animator> list) {
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(list);
        animatorSet.setInterpolator(new DecelerateInterpolator());
        animatorSet.start();
    }

    class ImageHolder extends RecyclerView.ViewHolder {

        private ImageView imageView;

        ImageHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.iv_itemimg);
        }
    }
}
